home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Franz PD / Franz PD Disk #001 (19xx)(Amiga User Group Deutschland e.V.).zip / Franz PD Disk #001 (19xx)(Amiga User Group Deutschland e.V.).adf / Clock / clock.c < prev    next >
C/C++ Source or Header  |  1986-10-22  |  5KB  |  157 lines

  1. /*
  2.  * clock - a dumb, digital clock in the upper right-hand corner. Designed
  3.  *    to be small, not flexible!
  4.  *
  5.  * Copyright (c) 1986, Mike Meyer
  6.  *
  7.  * Permission is hereby granted to distribute this program for any purposes
  8.  * whatsoever, so long as this notice, including the above copyright, is
  9.  * included with the distribution. Unlike other people, I don't care if you
  10.  * make money off of this, so long as I get credit for having written it.
  11.  */
  12.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <exec/tasks.h>
  16. #include <devices/timer.h>
  17. #include <libraries/dos.h>
  18. #include <intuition/intuition.h>
  19.  
  20. #include <stdio.h>
  21.  
  22. #define INTUITION_REV    1
  23. /*
  24.  * Things to tweak:
  25.  *    WIN_WIDTH - the width of the window output screen. Should be 369.
  26.  *    WAIT_TIME - how long to wait between updates. Also the maximum
  27.  *        mis-time you can get.  Finally, it's the longest period of
  28.  *        time you have to put up with the clock broken. Measured in
  29.  *        micro-seconds.
  30.  */
  31. #define    WIN_WIDTH    369
  32. #define WAIT_TIME    250000
  33.  
  34. static struct NewWindow    New_Window = {
  35.     (590 - WIN_WIDTH), 0,    /* Upper right, out of the way */
  36.     WIN_WIDTH, 10,        /* Just big enough for the time */
  37.     -1, -1,            /* Default pens */
  38.     CLOSEWINDOW,        /* All we care about is closing */
  39.     WINDOWCLOSE        /* Borderless, fairly standard window */
  40.     | WINDOWDEPTH | WINDOWDRAG | SMART_REFRESH | NOCAREREFRESH,
  41.     (struct Gadget *) NULL,
  42.     (struct Image *) NULL,
  43.     "",            /* Empty title */
  44.     (struct Screen *) NULL,
  45.     (struct BitMap *) NULL,
  46.     0, 0, 0, 0,        /* no change sizes, doesn't matter */
  47.     WBENCHSCREEN        /* Of course! */
  48.     } ;
  49.  
  50. static char    Date_Buffer[40] ;    /* Now you know where the time goes! */
  51.  
  52. static struct IntuiText    Date_Text = {
  53.     1, 0,                /* Use the standard pen colors */
  54.     JAM2,                /* Use both of them */
  55.     0, 0,                /* in the upper left-hand corner */
  56.     (struct TextAttr *) NULL,    /* Use default text */
  57.     Date_Buffer,            /* Buffer for time */
  58.     (struct IntuiText *) NULL    /* All of text */
  59.     } ;
  60.  
  61. struct IntuitionBase    *IntuitionBase ;
  62.  
  63. /*
  64.  * Some things that need to be shared with done.
  65.  */
  66. static struct Window        *Window = NULL ;
  67. static struct timerequest    Time_Req ;
  68. static struct MsgPort        *Timer_Port = NULL, *CreatePort() ;
  69. #ifdef    DEBUG
  70. static short            cli = FALSE ;
  71. #endif
  72.  
  73. #ifdef    DEBUG
  74. main(argc, argv) int argc; char *argv; {
  75. #else
  76. _main() {
  77. #endif
  78.     register short        hours, minutes ;
  79.     register short        chip_free, fast_free ;
  80.     struct DateStamp    now ;
  81.     struct IntuiMessage    *Msg, *GetMsg() ;
  82.     struct Task        *FindTask() ;
  83.     ULONG            AvailMem() ;
  84.  
  85. #ifdef    DEBUG
  86.     if (argc) cli = TRUE ;
  87. #endif
  88.     if ((IntuitionBase = (struct IntuitionBase *)
  89.         OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  90.         done(20, "Can't open Intuition library") ;
  91.  
  92.     if ((Timer_Port = CreatePort("Timer Port", 0)) == NULL)
  93.         done(20, "Can't create timer port") ;
  94.  
  95.     if (OpenDevice(TIMERNAME, UNIT_VBLANK, (char *) &Time_Req, 0) != NULL)
  96.         done(20, "Can't open timer device") ;
  97.     Time_Req . tr_node . io_Message . mn_ReplyPort = Timer_Port ;
  98.     Time_Req . tr_node . io_Command = TR_ADDREQUEST ;
  99.     Time_Req . tr_node . io_Flags = 0 ;
  100.     Time_Req . tr_node . io_Error = 0 ;
  101.  
  102.     if ((Window = (struct Window *) OpenWindow(&New_Window)) == NULL)
  103.         done(20, "Can't open window") ;
  104.     
  105.     /* Nudge me up to high priority */
  106.     (void) SetTaskPri(FindTask((char *) 0), 20) ;
  107.  
  108.     for (;;) {
  109.         DateStamp(&now) ;
  110.         chip_free = AvailMem(MEMF_CHIP) >> 10;
  111.         fast_free = AvailMem(MEMF_FAST) >> 10;
  112.         hours = now . ds_Minute / 60 ;
  113.         minutes = now . ds_Minute % 60 ;
  114.         /* Oh, this hurts. But stdio, here we come.... */
  115.         sprintf(Date_Buffer, " Chip:%3d  Fast:%4d  Time:%2d:%02d:%02d ",
  116.                     chip_free, fast_free, hours, minutes ,
  117.                     now . ds_Tick / TICKS_PER_SECOND) ;
  118.  
  119.         PrintIText(Window -> RPort, &Date_Text, 28, 1) ;
  120.  
  121.         Time_Req . tr_time . tv_secs = 0 ;
  122.         Time_Req . tr_time . tv_micro = WAIT_TIME ;
  123.         SendIO((char *) &Time_Req . tr_node) ;
  124.         Wait(1 << Window -> UserPort -> mp_SigBit
  125.            | 1 << Timer_Port -> mp_SigBit) ;
  126.  
  127.         while (Msg = GetMsg(Window -> UserPort)) {
  128.             if (Msg -> Class == CLOSEWINDOW) {
  129.                 ReplyMsg(Msg) ;
  130.                 done(0, "exit") ;
  131.                 }
  132.             ReplyMsg(Msg) ;
  133.             }
  134.  
  135.         (void) GetMsg(Timer_Port) ;
  136.         }
  137.     /* NOTREACHED */
  138.     }
  139. /*
  140.  * done - just clean up that which is open, and then leave.
  141.  */
  142. done(how, why) int how; char *why; {
  143.  
  144.     AbortIO((char *) &Time_Req . tr_node) ;
  145.     if (Window) CloseWindow(Window) ;
  146.     if (Time_Req . tr_node . io_Message . mn_ReplyPort)
  147.         CloseDevice(&Time_Req) ;
  148.     if (Timer_Port) DeletePort(Timer_Port) ;
  149.     if (IntuitionBase) CloseLibrary(IntuitionBase) ;
  150. #ifdef    DEBUG
  151.     if (cli) printf("clock: %s\n", why) ;
  152. #endif
  153.  
  154.     OpenWorkBench() ;            /* As requested */
  155.     exit(how) ;
  156.     }
  157.